package com.lst.servlet;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.FocusReqPara;
import com.lst.entity.response.FocusResPara;
import com.lst.model.Focus;
import com.lst.service.FocusService;
import com.lst.utils.RequestUtils;


/**
 * @ClassName: FocusMyListServlet
 * @Description: 关注详情接口
 * 
 */
public class FansMyListServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private FocusService focusService = ctx.getBean(FocusService.class);
	
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		FocusResPara resPara = new FocusResPara();

		try {
			FocusReqPara reqPara = getReqPara(request);
			Integer page = Integer.valueOf(reqPara.getPage());
			Integer pagesize = Integer.valueOf(reqPara.getPagesize());	
		
		if(isCheckField(reqPara,resPara)){
			Integer userid = Integer.valueOf(reqPara.getUserid());
			//根据id查询关注我详情列表
			Map<String, Object> reqMap = new HashMap<String, Object>();		
			reqMap.put("focusUser", userid);
			reqMap.put("isDelete", false);
			reqMap.put("orderByClause", "md.createDate desc");
    
			PageBounds pageBounds = new PageBounds(page,pagesize);
			List<Focus> focusList=focusService.queryByList3(reqMap, pageBounds);
			//查询自己是否关注过对方
			if(focusList!=null&&focusList.size()>0){
				for(Focus focus:focusList){
					
					Map<String, Object> map = new HashMap<String, Object>();
					
					map.put("focusUser",focus.getCreateUser());
					map.put("createUser",focus.getFocusUser());
		            map.put("isDelete", false);
					Focus focus2=focusService.selectByMap(map);
					if(focus2==null){
						focus.setIsFocus(false);
					}else{
						focus.setIsFocus(true);
					}
				}
			}
			
			//粉丝数量
			int totalFansNum=focusService.countFansTotal(userid);
			resPara.setTotalFansNum(totalFansNum);
			resPara.setFocusList(focusList);
			resPara.setCode(CommCode.M_Y000000);
			resPara.setMessage(CommCode.M_Y000001);
		  }
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}
		
		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
//			logger.info("FansMyListServlet jsonResult:" + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

//			logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public FocusReqPara getReqPara(HttpServletRequest request) {

		FocusReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new FocusReqPara());

			reqPara = (FocusReqPara) baseRequest.clone();

			String userid = request.getParameter("userid");
			String page = request.getParameter("page");
			String pagesize = request.getParameter("pagesize");
	
//			logger.info("reqPara userid:" + userid);
//			logger.info("reqPara page: " + page);
//			logger.info("reqPara pagesize: " + pagesize);

	        reqPara.setPage(page);
	        reqPara.setPagesize(pagesize);
	        reqPara.setUserid(userid);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		FocusReqPara cdReqPara = (FocusReqPara) reqPara;
		BaseResponse cdResPara = (BaseResponse) resPara;

		if (StringUtils.isEmpty(cdReqPara.getPage())){
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000009);	
		} else if (StringUtils.isEmpty(cdReqPara.getPagesize())){
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000010);				
		} else if (StringUtils.isEmpty(cdReqPara.getUserid())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000002);
		}  else {
			flag = true;
		}

		return  flag;
	}

}
